// packageA/pages/evaluate/index.js
const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
const WXAPI = require("../../../api/api");
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    iAnonymous: 0,
    checked: false,
    objStr: {},
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    title: "评价",
    dEvaluateScore1: 1,
    dEvaluateScore2: 1,
    fileList1: [],
    fileList2: [],
    fileIds1: [],
    fileIds2: [],
    sEvaluateContent: "",
    sEvaluateContent2: "",
  },
  onChangeStar1(e) {
    this.setData({
      dEvaluateScore1: e.detail,
    });
  },
  onChangeStar2(e) {
    this.setData({
      dEvaluateScore2: e.detail,
    });
  },
  submit() {
    let _this = this;
    wx.showModal({
      title: "提示",
      content: "确认提交评价？",
      complete: async (res) => {
        if (res.confirm) {
          let id = _this.data.objStr.id;
          let data = {
            iAnonymous: _this.data.iAnonymous,
            sOrgId: _this.data.objStr.sOrgId,
            sEvaluatePerson: App.globalData.userInfo.nickname,
            EvaluateProjects: [
              {
                sOrderId: _this.data.objStr.id,
                sProjectId: _this.data.objStr.project.sProjectId,
                dEvaluateScore: _this.data.dEvaluateScore1,
                sEvaluateContent: _this.data.sEvaluateContent1,
                sEvaluatePerson: App.globalData.userInfo.nickname,
                iAnonymous: _this.data.iAnonymous,
                Attachments: _this.data.fileIds1,
              },
            ],
            EvaluatePeople: {
              sOrderId: _this.data.objStr.id,
              dEvaluateScore: _this.data.dEvaluateScore2,
              sEvaluatePerson: App.globalData.userInfo.nickname,
              sServicePeopleId: _this.data.objStr.project.sServicePeopleId,
              sEvaluateContent: _this.data.sEvaluateContent2,
              iAnonymous: _this.data.iAnonymous,
              Attachments: _this.data.fileIds2,
            },
          };
          let res = await WXAPI.createServerEvaluate(id, data);
          if (res.data.code == 0) {
            wx.showToast({
              title: res.data.message,
            });
            // setTimeout(() => {
            //   wx.navigateTo({
            //     url: `/packageA/pages/all-order/index?orderType=2&navigationBarTitle=全部订单&status=0`,
            //   })
            // }, 2000);
            setTimeout(() => {
              var page = getCurrentPages();
              var prevPage = page[page.length - 2];
              // 存储上一页数据
              // prevPage.setData({
              //   orderType: 2,
              //   navigationBarTitle: "全部订单",
              //   status:0
              // });
              // 返回刷新
              prevPage.onLoad();
              wx.navigateBack({
                detail: 1,
              });
            }, 2000);
          }
        }
      },
    });
  },
  deleteFile(e) {
    let _this = this;
    const { index,type } = e.currentTarget.dataset;
    let obj = this.data.objStr;
    wx.showModal({
      title: "提示",
      content: "是否确认删除？",
      complete: (res) => {
        if (res.confirm) {
          const { id } = e.detail.file;
          let fileList1 = JSON.parse(JSON.stringify(this.data.fileList1))
          let fileList2 = JSON.parse(JSON.stringify(this.data.fileList2))
          let fileIds1 = JSON.parse(JSON.stringify(this.data.fileIds1))
          let fileIds2 = JSON.parse(JSON.stringify(this.data.fileIds2))
          if(type == 1){
            let i = fileList1.findIndex((item) => item.id == id);
            fileList1.splice(i, 1);
            let j = fileIds1.findIndex((item) => item == id);
            fileIds1.splice(j, 1);
          } else {
            let i = fileList2.findIndex((item) => item.id == id);
            fileList2.splice(i, 1);
            let j = fileIds2.findIndex((item) => item == id);
            fileIds2.splice(j, 1);
          }
          _this.setData({ fileList1, fileList2, fileIds1,fileIds2 });
        }
      },
    });
  },
  afterRead(event) {
    const { type } = event.currentTarget.dataset;
    const { file } = event.detail;
    let _this = this;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    let typeValue = type == 1 ? 'OrderEvaluateProject' : 'OrderEvaluatePeople'
    console.log('typeValue',typeValue);
    wx.uploadFile({
      url: baseUrl["baseUrl"] + "/api/Files", //服务器端接收图片的路径
      filePath: file.url,
      name: "file",
      header: { Authorization: wx.getStorageSync("token") },
      formData: {
        name: `test${new Date().getTime()}.png`,
        sBusinessType: typeValue,
      },
      success(res) {
        let data = JSON.parse(res.data).data;
        // 上传完成需要更新 fileList
        if (type == 1) {
          const { fileList1 = [], fileIds1 = [] } = _this.data;
          fileList1.push({
            ...file,
            url: _this.data.imgBaseUrl + data.sFileUrl,
          });
          fileIds1.push(data.sId);
          _this.setData({ fileList1, fileIds1 });
        } else {
          const { fileList2 = [], fileIds2 = [] } = _this.data;
          fileList2.push({
            ...file,
            url: _this.data.imgBaseUrl + data.sFileUrl,
          });
          fileIds2.push(data.sId);
          _this.setData({ fileList2, fileIds2 });
        }
      },
    });
  },
  onChange(e) {
    this.setData({
      checked: e.detail,
    });
    this.setData({
      iAnonymous: this.data.checked ? 1 : 0,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.type) {
      wx.setNavigationBarTitle({
        title: options.type == "1" ? "商品评价" : "服务评价",
      });
    }
    if (options.objStr) {
      this.setData({
        objStr: JSON.parse(options.objStr),
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
